package com.syq.face.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.syq.face.MyApplication;
import com.syq.face.R;
import com.syq.face.adapter.GroupAdapter;
import com.syq.face.model.JsonResult;
import com.syq.face.model.ManagementGroupInfo;
import com.syq.face.utils.RetrofitUtil;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link GroupFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class GroupFragment extends NotifiedFragment {


    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    public static final String TAG = "GroupFragment";


    private String mParam1;
    private String mParam2;
    private String username;

    private RecyclerView recyclerView;
    private GroupAdapter groupAdapter;

    public void setUsername(String username) {
        this.username = username;
    }

    public GroupFragment() {

    }

    public static GroupFragment newInstance(String param1, String username) {
        GroupFragment fragment = new GroupFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setUsername(username);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_group, container, false);
        recyclerView = rootView.findViewById(R.id.fragment_group_rv);
        init();
        return rootView;
    }

    private void init() {
        initAdapter();
        recyclerView.setAdapter(groupAdapter);
        Log.d(TAG, "init: ");
    }

    private void initAdapter() {
        groupAdapter = new GroupAdapter();
        groupAdapter.setOnItemClickListener(new GroupAdapter.MyViewHolder.OnItemClickListener() {
            @Override
            public void onItemClick(int position, String groupId) {

                //TODO
                AttendanceFragment attendanceFragment = AttendanceFragment.newInstance(groupId);

                Log.d(TAG, "onItemClick: groupId"+groupId);

                MyApplication.getRootFragment().start(attendanceFragment);
            }
        });
    }


    @Override
    public void notifyDataReload() {
        Log.d(TAG, "notifyDataReload: ");
        RetrofitUtil.getInstance().getUserService().getGroups(MyApplication.getMyself().getToken())
                .enqueue(new Callback<JsonResult<List<ManagementGroupInfo>>>() {
                    @Override
                    public void onResponse(Call<JsonResult<List<ManagementGroupInfo>>> call, Response<JsonResult<List<ManagementGroupInfo>>> response) {
                        JsonResult<List<ManagementGroupInfo>> body = response.body();
                        Log.d(TAG, "onResponse: token:"+MyApplication.getMyself().getToken());
                        Log.d(TAG, "onResponse: body:"+new Gson().toJson(body));
                        Log.d(TAG, "onResponse: " + new Gson().toJson(body));
                        List<ManagementGroupInfo> data = body.getData();
///                       List<ManagementGroupInfo> data = new ArrayList<>();
//                        data.add(new ManagementGroupInfo("10", "qwq1", "dddd", "admin", "qwqa", 1, "123"));
//                        data.add(new ManagementGroupInfo("11", "qwq2", "dddd", "admin", "qwqa", 1, "123"));
//                        data.add(new ManagementGroupInfo("12", "qwq3", "dddd", "admin", "qwqa", 1, "123"));
//                        data.add(new ManagementGroupInfo("13", "qwq4", "dddd", "admin", "qwqa", 1, "123"));
                          Log.d(TAG, "onResponse: getData:" + data);
                        groupAdapter.setManagementGroupInfoList(data);
                        groupAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFailure(Call<JsonResult<List<ManagementGroupInfo>>> call, Throwable t) {

                    }
                });
    }
}